package com.sc.vipface.tasks;

import com.sc.vipface.entity.dto.AdapterDto;
import com.sc.vipface.services.IotPlatformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class HeartBeatTask extends Thread {
    private AdapterDto dto;

    @Autowired
    private IotPlatformService iotPlatformService;

    private String heartBeatUrl;

    public HeartBeatTask() {

    }

    public void init(AdapterDto dto, String heartBeatUrl) {
        this.dto = dto;
        this.heartBeatUrl = heartBeatUrl;
    }

    public void updateDto(AdapterDto dto) {
        this.dto = dto;
    }

    public void run() {
        while (true) {
            if (dto != null && heartBeatUrl != null) {
                iotPlatformService.heartBeatAsync(heartBeatUrl, dto);
            }

            try {
                Thread.sleep(1000 * 30);
            } catch (Exception ex) {
                log.warn("Sleep exception : " + ex.getMessage());
            }
        }
    }
}
